package controller

import (
	"ruanGongKeShe16Zu/db"
	"ruanGongKeShe16Zu/model"
	"ruanGongKeShe16Zu/response"
	"ruanGongKeShe16Zu/service"
	"ruanGongKeShe16Zu/service/vo"
	"strconv"

	"github.com/gin-gonic/gin"
)

type ICommentController interface {
	GeneralController
	ListComment(ctx *gin.Context)
	SelectByInstrumentID(ctx *gin.Context)
}

type CommentController struct {
	Service service.CommentService
}

func NewCommentController() ICommentController {
	service := service.GetCommentService()
	return CommentController{
		Service: *service,
	}
}

func (c CommentController) Create(ctx *gin.Context) {
	var req vo.CommentReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.Fail(ctx, gin.H{"error": err.Error()}, "数据验证错误")
		return
	}
	err1 := db.GetDB().Model(&model.Comment{}).Where("user_id = ? AND instrument_id = ?", req.UserID, req.InstrumentID).First(&model.Comment{})
	if err1.Error == nil {
		response.Fail(ctx, nil, "不能重复评论！")
		return
	}
	resp, err := c.Service.Create(ctx, &req)
	if err != nil {
		// utils.LogrusObj.Infoln(err)
		response.Fail(ctx, nil, "创建失败")
		return
	}
	response.Success(ctx, gin.H{"comment": resp}, "创建成功")
}

func (c CommentController) Retrieve(ctx *gin.Context) {
	//获取path中的参数
	commentId, _ := strconv.Atoi(ctx.Params.ByName("id"))

	comment, err := c.Service.Retrieve(ctx, commentId)
	if err != nil {
		response.Fail(ctx, nil, "评论不存在")
		return
	}
	response.Success(ctx, gin.H{"comment": comment}, "查询成功")
}
func (c CommentController) Update(ctx *gin.Context) {
	//绑定body中的参数
	var requestComment vo.CommentReq
	if err := ctx.ShouldBind(&requestComment); err != nil {
		response.Fail(ctx, nil, "数据验证错误")
		return
	}
	//获取path中的参数
	commentId, _ := strconv.Atoi(ctx.Params.ByName("id"))

	updateComment, err := c.Service.Retrieve(ctx, commentId)
	if err != nil {
		response.Fail(ctx, nil, "评论不存在")
		return
	}

	//更新分类
	//可传map,struct,name value
	comment, err := c.Service.Update(ctx, updateComment, &requestComment)
	if err != nil {
		response.Fail(ctx, nil, "更新失败")
		return
	}
	response.Success(ctx, gin.H{"comment": comment}, "更新成功")
}

func (c CommentController) Delete(ctx *gin.Context) {
	//获取path中的参数
	commentId, _ := strconv.Atoi(ctx.Params.ByName("id"))

	_, err := c.Service.Retrieve(ctx, commentId)
	if err != nil {
		response.Fail(ctx, nil, "评论不存在")
		return
	}

	err = c.Service.Delete(ctx, commentId)
	if err != nil {
		response.Fail(ctx, nil, "删除失败，请重试")
		return
	}

	response.Success(ctx, nil, "删除成功")
}

func (c CommentController) ListComment(ctx *gin.Context) {
	var req vo.ListCommentReq
	if err := ctx.ShouldBind(&req); err != nil {
		// 参数校验
		// utils.LogrusObj.Infoln(err)
		response.Fail(ctx, nil, "参数不能为空!")
		return
	}

	resp, err := c.Service.CommentList(ctx, &req)
	if err != nil {
		// utils.LogrusObj.Infoln(err)
		response.Fail(ctx, nil, "查询失败")
		return
	}
	response.Success(ctx, gin.H{"Comments": resp}, "查询成功")
}

func (c CommentController) SelectByInstrumentID(ctx *gin.Context) {
	//获取path中的参数
	userID, _ := strconv.Atoi(ctx.Params.ByName("id"))
	var req vo.ListCommentReq
	if err := ctx.ShouldBind(&req); err != nil {
		// 参数校验
		// utils.LogrusObj.Infoln(err)
		response.Fail(ctx, nil, "参数不能为空!")
		return
	}

	resp, err := c.Service.SelectByUserID(ctx, &req, uint(userID))
	if err != nil {
		// utils.LogrusObj.Infoln(err)
		response.Fail(ctx, nil, "查询失败")
		return
	}
	response.Success(ctx, gin.H{"Comments": resp}, "查询成功")
}
